<?php
session_start();
    if (isset($_SESSION['user'])) { header('Location: game.php'); }

include('Util/Util.php');
include('Classes/Account.php');
$account = new Account();


if (isset($_POST['user'])) {
    if ($account->ifAccountExists($_POST['user'])) {
            $_SESSION['user'] = $_POST['user'];
            header('Location: game.php');
    } else {
        $error_message = "Your account doesn't exist. Please register above. <br />";
    }
}

display('header');

?>

    <body>

        <h2>Create an Account</h2><br />

        <?php

        if (isset($_POST['submit'])) {
            if (isset($_POST['nationName']) && isset($_POST['userName'])) {
                    if (!$account->ifAccountExists($_POST['userName'])) {
                        if ($account->createAccount($_POST['userName'], $_POST['nationName'])) {
                            echo "Success! Please log in below. <br />";
                        } else {
                            echo "Sorry, please try again. <br />";
                        }
                    } else {
                        echo "Sorry, your username has been taken. Please choose another one. <br />";
                    }
            } else {
                echo "Sorry, you need to fill in both fields. <br />";
            }
        }
        ?>
        <form action="index.php" method="post">
            
            Name of Nation: <input type="text" name="nationName"/>
            <br />
            Your User Name: <input type="text" name="userName"/>
            <br />
            <input type="submit" name ="submit" value="Start Game!" />
        </form>
        <br /><br />

        <!-- Login Portion -->

<?php echo $error_message; ?>

        <h2>Existing Account</h2>
        <form action="index.php" method="post">
             Your User Name: <input type="text" name="user" />
             <br />
             <input type="submit" value="Start Game!" />
            
        </form>
        
    </body>
</html>
